package controllers.administrator;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import services.AdministratorService;
import controllers.AbstractController;
import domain.Administrator;
import forms.AdministratorForm;

@Controller
@RequestMapping("/security/registration")
public class RegisterAdministratorController extends AbstractController {

	@Autowired
	private AdministratorService administratorService;

	// Creation
	@RequestMapping(value = "/administrator", method = RequestMethod.GET)
	public ModelAndView registration() {
		ModelAndView result;
		AdministratorForm administratorForm;
		Boolean isAdministrator;

		isAdministrator = true;
		administratorForm = new AdministratorForm();

		result = new ModelAndView("security/registration/administrator");
		result.addObject("administratorForm", administratorForm);
		result.addObject("isAdministrator", isAdministrator);

		return result;
	}

	// Edition
	@RequestMapping(value = "/administrator", method = RequestMethod.POST, params = "save")
	public ModelAndView save(@Valid AdministratorForm administratorForm,
			BindingResult binding) {
		ModelAndView result;
		Administrator administrator;
		Boolean isAdministrator;

		if (binding.hasErrors()) {
			result = createEditModelAndView(administratorForm);
		} else {
			try {
				if (!administratorForm.getAcceptConditions()) {
					result = createEditModelAndView(administratorForm,
							"security.commit.noAcceptConditions");
				} else if (!administratorForm.getPassword().equals(
						administratorForm.getRepeatPassword())) {
					result = createEditModelAndView(administratorForm,
							"security.commit.passwordDifferents");
				} else {

					administrator = administratorService.reconstruct(administratorForm);

					administratorService.save(administrator);
					result = new ModelAndView("redirect:../../");
				}
			} catch (Throwable oops) {

				result = createEditModelAndView(administratorForm,
						"administrator.commit.error");
			}
		}
		isAdministrator = true;
		result.addObject("isAdministrator", isAdministrator);
		return result;
	}

	// Ancillary methods
	protected ModelAndView createEditModelAndView(
			AdministratorForm administratorForm) {
		ModelAndView result;

		result = createEditModelAndView(administratorForm, null);

		return result;
	}

	protected ModelAndView createEditModelAndView(
			AdministratorForm administratorForm, String message) {
		ModelAndView result;

		result = new ModelAndView("security/registration/administrator");

		result.addObject("administratorForm", administratorForm);
		result.addObject("message", message);

		return result;
	}
}